﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using F_MODEL;
using System.Data.SqlClient;
using System.Data;
namespace F_DAL
{
    public class UserAddrDal
    {
        /// <summary>
        /// 添加收货/送货地址
        /// </summary>
        /// <returns></returns>
        public bool UserAddrAdd(F_MODEL.UserAddrModel u)
        {
            SqlParameter[] par ={
                           new SqlParameter("@UserAddrName",SqlDbType.VarChar,50){Value=u.UserAddrName},
                           new SqlParameter("@UserAddrTel",SqlDbType.VarChar,50){Value=u.UserAddrTel},
                           new SqlParameter("@UserAddrCon",SqlDbType.VarChar,50){Value=u.UserAddrCon},
                           new SqlParameter("@UserAddrCity",SqlDbType.VarChar,50){Value=u.UserAddrCity},
                            new SqlParameter("@UserAddrAdr",SqlDbType.VarChar,50){Value=u.UserAddrAdr},
                             new SqlParameter("@UserId",SqlDbType.Int){Value=u.Userid},
                             new SqlParameter("@UserAddrState",SqlDbType.Int){Value=u.UserAddrState}
                           };
            return DbHelperSQL.RunProcedureReturnBool("ProUserAddr", par);

        }
        /// <summary>
        /// 地址查询
        /// </summary>
        /// <returns></returns>
        public List<UserAddrModel> UserAddrSel(int UserId)
        {
            SqlParameter[] par ={
                               new SqlParameter("@UserId",SqlDbType.Int){Value=UserId}
                               };
            List<UserAddrModel> list = new List<UserAddrModel>();

            SqlDataReader dt = DbHelperSQL.RunProcedure("ProUserAddrSel", par);
            while (dt.Read())
            {
                UserAddrModel um = new UserAddrModel();
                um.UserAddrId = int.Parse(dt["UserAddrId"].ToString());
                um.UserAddrName = dt["UserAddrName"].ToString();
                um.UserAddrTel = dt["UserAddrTel"].ToString();
                um.UserAddrCon = dt["UserAddrCon"].ToString();
                um.UserAddrCity = dt["UserAddrCity"].ToString();
                um.UserAddrAdr = dt["UserAddrAdr"].ToString();
                um.Userid = int.Parse(dt["UserId"].ToString());
                um.UserAddrState = int.Parse(dt["UserAddrState"].ToString());
                list.Add(um);
            }
            return list;
        }
        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public int UserAddrDel(int UserId)
        {
            string sql = string.Format("delete from UserAddr where  UserAddrId=" + UserId + "");
            return DbHelperSQL.ExecuteSql(sql);
        }
        /// <summary>
        /// 添加运单信息
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public bool MailsAdd(UserAddrModel u)
        {
            SqlParameter[] par ={
                           new SqlParameter("@mailId",SqlDbType.VarChar,50){Value=u.mailId},
                           new SqlParameter("@sendUser",SqlDbType.VarChar,50){Value=u.sendUser},
                           new SqlParameter("@receiveUser",SqlDbType.VarChar,50){Value=u.receiveUser},
                           new SqlParameter("@dweight",SqlDbType.VarChar,50){Value=u.dweight},
                            new SqlParameter("@predictPrice",SqlDbType.Decimal){Value=u.predictPrice},
                             new SqlParameter("@goodsImg",SqlDbType.VarChar,50){Value=u.goodsImg},
                             new SqlParameter("@mailIsDel",SqlDbType.Int){Value=u.mailIsDel},
                             new SqlParameter("@mailTimeSpan",SqlDbType.DateTime){Value=u.mailTimeSpan},
                             new SqlParameter("@mailStatus",SqlDbType.Int){Value=u.mailStatus},
                             new SqlParameter("@stationId",SqlDbType.Int){Value=u.stationId},
                                    new SqlParameter("@ParCeId",SqlDbType.Int){Value=u.ParCeId},
                                     new SqlParameter("@Userid",SqlDbType.Int){Value=u.Userid},
                                     new SqlParameter("@types1",SqlDbType.VarChar){Value=u.types1}
                           };
            return DbHelperSQL.RunProcedureReturnBool("Pro_Mails", par);
        }
        /// <summary>
        /// 提交寄件信息
        /// </summary>
        /// <param name="Userid"></param>
        /// <returns></returns>
        public List<UserAddrModel> MailSel(int Userid)
        {
            SqlParameter[] par ={
                                new SqlParameter("@Userid",SqlDbType.Int){Value=Userid}
                            };
            SqlDataReader read = DbHelperSQL.RunProcedure("Pro_MailSel", par);
            List<UserAddrModel> list = new List<UserAddrModel>();
            while (read.Read())
            {
                UserAddrModel um = new UserAddrModel();
                um.types1 = read["types1"].ToString();
                um.UserAddrTel = read["UserAddrTel"].ToString();
                um.UserAddrCon = read["UserAddrCon"].ToString();
                um.UserAddrCity = read["UserAddrCity"].ToString();
                um.UserAddrName = read["UserAddrName"].ToString();
                um.UserAddrAdr = read["UserAddrAdr"].ToString();
                um.cnTel = read["cnTel"].ToString();
                um.name = read["name"].ToString();
                um.mailId = read["mailId"].ToString();
                um.sendUser = read["sendUser"].ToString();
                um.receiveUser = read["receiveUser"].ToString();
                um.dweight = read["dweight"].ToString();
                um.predictPrice = Convert.ToDecimal(read["predictPrice"].ToString());
                um.goodsImg = read["goodsImg"].ToString();
                um.mailIsDel = int.Parse(read["mailIsDel"].ToString());
                um.mailTimeSpan = Convert.ToDateTime(read["mailTimeSpan"].ToString());
                um.mailStatus = int.Parse(read["mailStatus"].ToString());
                um.stationId = int.Parse(read["stationId"].ToString());
                um.ParCeId = int.Parse(read["ParCeId"].ToString());
                um.Userid = int.Parse(read["Userid"].ToString());
                list.Add(um);
            }
            return list;

        }


        /// <summary>
        /// 根据当前登录用户，查询
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="mailStatus"></param>
        /// <returns></returns>
        public List<UserAddrModel> SelectState(int userid)
        {
            string sql = string.Format("select * from Mails where Userid='{0}' and mailStatus < 4 ", userid);
            return DBhelpers.GetList<UserAddrModel>(sql);
        }

        /// <summary>
        /// 用户接受包裹，修改状态
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public int UpdateState(int userid, int mailId)
        {
            string sql = string.Format("update Mails set mailStatus = 4 where Userid=" + userid + " and mailId=" + mailId + "  ");
            return DBhelpers.ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 后台审核寄件信息
        /// </summary>
        /// <param name="Userid"></param>
        /// <returns></returns>
        public List<UserAddrModel> MaiUserlSel(int mailStatus)
        {
            SqlParameter[] par ={
                                new SqlParameter("@mailStatus",SqlDbType.Int){Value=mailStatus}
                            };
            SqlDataReader read = DbHelperSQL.RunProcedure("Pro_MailUserSel", par);
            List<UserAddrModel> list = new List<UserAddrModel>();
            while (read.Read())
            {
                UserAddrModel um = new UserAddrModel();
                um.types1 = read["types1"].ToString();
                um.name = read["name"].ToString();
                um.mailId = read["mailId"].ToString();
                um.sendUser = read["sendUser"].ToString();
                um.receiveUser = read["receiveUser"].ToString();
                um.dweight = read["dweight"].ToString();
                um.predictPrice = Convert.ToDecimal(read["predictPrice"].ToString());
                um.goodsImg = read["goodsImg"].ToString();
                um.mailIsDel = int.Parse(read["mailIsDel"].ToString());
                um.mailTimeSpan = Convert.ToDateTime(read["mailTimeSpan"].ToString());
                um.mailStatus = int.Parse(read["mailStatus"].ToString());
                um.stationId = int.Parse(read["stationId"].ToString());
                um.ParCeId = int.Parse(read["ParCeId"].ToString());
                list.Add(um);
            }
            return list;
        }
        /// <summary>
        /// 修改寄件状态信息
        /// </summary>
        /// <param name="mailStatus"></param>
        /// <param name="mailId"></param>
        /// <returns></returns>
        public int UserMailUpd(int mailStatus,int mailId)
        {
            string sql = string.Format("update Mails set mailStatus=" + mailStatus + " where mailId=" + mailId + "");
            return DbHelperSQL.ExecuteSql(sql);
        }

        /// <summary>
        /// 包裹信息
        /// </summary>
        /// <param name="Userid"></param>
        /// <returns></returns>
        public List<UserAddrModel> MaildaihuoSel(int mailId)
        {
            SqlParameter[] par ={
                                new SqlParameter("@mailId",SqlDbType.VarChar){Value=mailId}
                            };
            SqlDataReader read = DbHelperSQL.RunProcedure("Pro_MaildaihuoSel", par);
            List<UserAddrModel> list = new List<UserAddrModel>();
            while (read.Read())
            {
                UserAddrModel um = new UserAddrModel();
                um.types1 = read["types1"].ToString();
                um.name = read["name"].ToString();
                um.mailId = read["mailId"].ToString();
                um.sendUser = read["sendUser"].ToString();
                um.receiveUser = read["receiveUser"].ToString();
                um.dweight = read["dweight"].ToString();
                um.predictPrice = Convert.ToDecimal(read["predictPrice"].ToString());
                um.goodsImg = read["goodsImg"].ToString();
                um.mailIsDel = int.Parse(read["mailIsDel"].ToString());
                um.mailTimeSpan = Convert.ToDateTime(read["mailTimeSpan"].ToString());
                um.mailStatus = int.Parse(read["mailStatus"].ToString());
                um.stationId = int.Parse(read["stationId"].ToString());
                um.ParCeId = int.Parse(read["ParCeId"].ToString());
                list.Add(um);
            }
            return list;
        }

        /// <summary>
        /// 包裹明细信息
        /// </summary>
        /// <param name="Userid"></param>
        /// <returns></returns>
        public List<UserAddrModel> MingxiSel(int mailId)
        {
            SqlParameter[] par ={
                                new SqlParameter("@mailId",SqlDbType.VarChar){Value=mailId}
                            };
            SqlDataReader read = DbHelperSQL.RunProcedure("Mingxi_All", par);
            List<UserAddrModel> list = new List<UserAddrModel>();
            while (read.Read())
            {
                UserAddrModel um = new UserAddrModel();
                um.mailId = read["mailId"].ToString();
                um.types1 = read["types1"].ToString();
                um.destination = read["destination"].ToString();
                um.WorkManTel = read["WorkManTel"].ToString();
                um.flightInfo = read["flightInfo"].ToString();
                um.WorkMan = read["WorkMan"].ToString();
                um.sendUser = read["sendUser"].ToString();
                um.receiveUser = read["receiveUser"].ToString();
                um.dweight = read["dweight"].ToString();
                um.predictPrice = Convert.ToDecimal(read["predictPrice"].ToString());
                um.goodsImg = read["goodsImg"].ToString();
                um.goodsName = read["goodsName"].ToString();
                um.goodsNum =int.Parse(read["goodsNum"].ToString());
                um.mailIsDel = int.Parse(read["mailIsDel"].ToString());
                um.mailTimeSpan = Convert.ToDateTime(read["mailTimeSpan"].ToString());
                um.mailStatus = int.Parse(read["mailStatus"].ToString());
                um.ParCeId = int.Parse(read["ParCeId"].ToString());
                list.Add(um);
            }
            return list;
        }
        /// <summary>
        /// 全部订单
        /// </summary>
        /// <returns></returns>
        public List<UserAddrModel> Pro_quanbu()
        {
            SqlDataReader read = DbHelperSQL.RunProcedure("Pro_quanbu");
            List<UserAddrModel> list = new List<UserAddrModel>();
            while (read.Read())
            {
                UserAddrModel um = new UserAddrModel();
                um.mailId = read["mailId"].ToString();
                um.types1 = read["types1"].ToString();
                um.cnTel = read["cnTel"].ToString();
                um.name = read["name"].ToString();
                list.Add(um);
            }
            return list;
        }

    }
}
